<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="false">
        <el-button slot="after" size="small" type="primary" @click="showDialog()">添加权限</el-button>
      </page-tools>
      <el-table :data="tableData" border default-expand-all row-key="id">
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="标识" prop="code"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.type === 1"
              size="mini"
              type="primary"
              plain
              @click="showDialog(scope.row)"
              >添加</el-button
            >
            <el-button size="mini" type="success" plain @click="showEditDialog(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="50%"
        @close="closeDialog"
      >
        <el-form ref="fromDataRef" :rules="rules" :model="fromData" label-width="80px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="fromData.name"></el-input>
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="fromData.code"></el-input>
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="fromData.description"></el-input>
          </el-form-item>
          <el-form-item label="企业可见">
            <el-switch v-model="fromData.enVisible" active-text="可见" inactive-text="不可见" active-value="1" inactive-value="0"></el-switch>
          </el-form-item>
          <!-- <el-form-item label="菜单顺序">
            <el-input v-model="fromData.name"></el-input>
          </el-form-item>
          <el-form-item label="菜单icon">
            <el-input v-model="fromData.name"></el-input>
          </el-form-item> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="postPermission"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, addPermission, getPermissionDetail, updatePermission } from "@/api/permission";
import { tranListToTreeData } from "@/utils/toTreedata";
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      fromData: {
        enVisible: '',
        name: '',
        code: '',
        description: '',
        type: 1, // type=1是第一层=2是第二层3是第三层
        pid: '0'
      },
      rules: {
        name: [{ required: true, message: '请填写权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请填写权限标识', trigger: 'blur' }],
        description: [{ required: true, message: '请填写权限描述', trigger: 'blur' }]
      }
    };
  },
  computed: {
    title: function() {
      if (this.fromData.id) {
        return '编辑权限'
      }
      return '添加权限'
    }
  },
  created() {
    this.getPermisiionList();
  },
  methods: {
    async getPermisiionList() {
      const res = await getPermissionList();
      this.tableData = tranListToTreeData(res, "0");
      console.log(this.tableData);
    },
    async showDialog(data) {
      if (data) {
        this.fromData.pid = data.id
        this.fromData.type = data.type
      }
      this.dialogVisible = true
    },
    async showEditDialog(data) {
      this.fromData = await getPermissionDetail(data.id)
      this.dialogVisible = true
    },
    async postPermission() {
      await this.$refs.fromDataRef.validate()
      if (this.fromData.id) {
        // 编辑
        await updatePermission(this.fromData)
      }
      await addPermission(this.fromData)
      this.$message.success('操作成功')
      this.getPermisiionList()
      this.dialogVisible = false
    },
    closeDialog() {
      this.dialogVisible = false
      this.$refs.fromDataRef.resetFields()
      this.fromData = {
        enVisible: '',
        name: '',
        code: '',
        description: '',
        type: 1, // type=1是第一层=2是第二层
        pid: ''
      }
    }
  }
};
</script>

<style></style>
